//定义路由转发器
import {createRouter,createWebHistory} from "vue-router"
import {getJwt} from "@/api/jwt.js";

//定义路由
const routes= [
    {
        name:"main",
        path:"/main",
        component:()=>import("@/components/view/Main.vue"),
        redirect: "/main/dashboard",
        children:[
            {
                name:"member",
                path:"/main/member",
                component:()=>import("@/components/view/Member.vue")
            },
            {
                name:"dashboard",
                path:"/main/dashboard",
                component:()=>import("@/components/view/Dashboard.vue")
            },
            {
                name:"user",
                path:"/main/user",
                component:()=>import("@/components/view/User.vue")
            },{
                name:"order",
                path:"/main/orders",
                component:()=>import("@/components/view/Order.vue")
            },{
                name:"goods",
                path:"/main/goods",
                component:()=>import("@/components/view/Good.vue")
            }
        ],

    },
    {
        name:"index",
        path:"/",
        redirect:"/main/dashboard"
    },{
        name:"login",
        path:"/login",
        component:()=>import("@/components/view/Login.vue")
    }
];

//创建路由转发器
const router=createRouter({
    routes,
    history:createWebHistory()
});


//路由守卫
router.beforeEach((to,from,next)=>{
    let jwt=getJwt();
    if(jwt){
        if(to.name==="login")
        {
            next("/main/member");
        }else{
            next();
        }
    }else {
        if(to.name!=="login")
        {
            next("/login");
        }else{
            next();
        }
    }
})

//导出路由转发器
export default router;